/*
 * @Author: your name
 * @Date: 2021-08-30 16:31:41
 * @LastEditTime: 2021-08-30 20:24:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day06\8-数组案例.js
 */
//在头部新增任意多个数据，并且返回最终数组长度

var arr4 = new Array(100, 200, 300, 400);
Array.prototype.myHeadAdd = function() {
    //1.新增的值依次插入
    //1.1新增长度值+1
    //1.2后序遍历 1-max
    //1.2.1 当前空间=前一个空间的值
    //    arr[i]=arr[i-1]
    //1.3 arr[0]=新增的值
    //arguments.length-1表示的是把新增的数用数组进行封装
    for (var key = arguments.length - 1; key >= 0; key--) {
        var newvalue = arguments[key]; //arguments.length传过来的实参的个数
        this.length++;
        for (var i = this.length - 1; i >= 1; i--) {
            this[i] = this[i - 1]; //数组中的值全部往后移
        }
        this[0] = newvalue;
    }
    return this.length;
}
arr4.myHeadAdd('hello');
console.log(arr4);
arr4.myHeadAdd('briup', 22.22, 'abc');
console.log(arr4);

//在数组的头部删除数据,返回删除的单个值

Array.prototype.myHeadRm = function() {
    //1.re=arr[0]
    //2.正序遍历数组,将arr[i]=arr[i+1] 0-max-1
    //3.长度减一
    var result = this[0];
    for (var i = 0; i <= this.length - 1; i++) {
        this[i] = this[i + 1];
    }
    this.length--;
    return result;
}
console.log(arr4.myHeadRm()); //briup
console.log(arr4.myHeadRm()); //22.22
console.log(arr4.myHeadRm()); //abc
console.log(arr4); //[ 'hello', 100, 200, 300, 400 ]

//在数组的末尾删除数据,减少数组的length值，返回移除的项
//1.找出最后一项,并删除
//2.长度减一
Array.prototype.myPop = function() {
    var result = this[this.length - 1];
    // for (i = this.length - 2; i > 0; i--) {
    //     this[i - 1] = this[i];
    // }
    this.length--;
    return result;
}
console.log(arr4.myPop());
console.log(arr4);